package com.monkey.demolist.tree.bean;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 * Author: Archur
 * Date: 2017-10-20
 * Time: 11:18
 */


public class Node {

    /**
     * 该节点的id
     */
    private int id;
    /**
     * 父节点(根节点的pId = 0)
     */
    private int pId;
    /**
     * 节点名
     */
    private String name;

    /**
     * 树的层级
     */
    private int level;
    /**
     * 是否展开
     */
    private boolean isExpand;
    /**
     * 图标
     */
    private int icon;
    /**
     * 父节点(一个)
     */
    private Node parent;
    /**
     * 子节点
     */
    private List<Node> children = new ArrayList<>();

    public Node(int id, int pId, String name) {
        this.id = id;
        this.pId = pId;
        this.name = name;
    }

    /**
     * 判断是否为根节点
     *
     * @return
     */
    public boolean isRoot() {
        return parent == null;
    }

    /**
     * 判断当前父节点是否是展开
     * 展开才显示子节点列表
     * 不展开则不显示子节点列表
     *
     * @return
     */
    public boolean isParentExpand() {
        if (parent == null) return false;
        return parent.isExpand();
    }

    /**
     * 判断当前节点是否是叶子节点(有无子节点)
     * 没有子节点的节点不显示图标
     *
     * @return
     */
    public boolean isLeaf() {
        return children.size() == 0;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getpId() {
        return pId;
    }

    public void setpId(int pId) {
        this.pId = pId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    /**
     * 得到当前节点的level
     * @return
     */
    public int getLevel() {
        return parent == null ? 0 : parent.getLevel() + 1;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public boolean isExpand() {
        return isExpand;
    }

    /**
     * 某个节点收缩,他的所有子节点全部收缩
     * @param expand
     */
    public void setExpand(boolean expand) {
        isExpand = expand;
        if (!isExpand){
            for (Node node:children){
                node.setExpand(false);
            }
        }
    }

    public int getIcon() {
        return icon;
    }

    public void setIcon(int icon) {
        this.icon = icon;
    }

    public Node getParent() {
        return parent;
    }

    public void setParent(Node parent) {
        this.parent = parent;
    }

    public List<Node> getChildren() {
        return children;
    }

    public void setChildren(List<Node> children) {
        this.children = children;
    }
}
